package org.ciallo.simplemusicplayer.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import org.ciallo.simplemusicplayer.R;
import org.ciallo.simplemusicplayer.data.model.PlaylistInfo;

import java.util.Locale;

/**
 * @author Jukomu
 * @Description: 歌单管理页面的 RecyclerView 适配器
 * @Package: org.ciallo.simplemusicplayer.adapter
 * @Project: SimpleMusicPlayer
 * @name: PlaylistManagementAdapter
 * @Date: 2025/6/9-23:04
 * @Filename: PlaylistManagementAdapter
 */
public class PlaylistManagementAdapter extends ListAdapter<PlaylistInfo, PlaylistManagementAdapter.PlaylistManageViewHolder> {

    private final OnPlaylistManageClickListener clickListener;
    private final OnPlaylistOptionsClickListener optionsClickListener;

    public interface OnPlaylistManageClickListener {
        void onPlaylistManageClick(PlaylistInfo playlistInfo); // 点击整个歌单项
    }

    public interface OnPlaylistOptionsClickListener {
        void onPlaylistOptionsClick(PlaylistInfo playlistInfo, View anchorView); // 点击更多操作按钮
    }

    public PlaylistManagementAdapter(OnPlaylistManageClickListener clickListener, OnPlaylistOptionsClickListener optionsClickListener) {
        super(DIFF_CALLBACK);
        this.clickListener = clickListener;
        this.optionsClickListener = optionsClickListener;
    }

    private static final DiffUtil.ItemCallback<PlaylistInfo> DIFF_CALLBACK = new DiffUtil.ItemCallback<PlaylistInfo>() {
        @Override
        public boolean areItemsTheSame(@NonNull PlaylistInfo oldItem, @NonNull PlaylistInfo newItem) {
            return oldItem.id == newItem.id;
        }

        @Override
        public boolean areContentsTheSame(@NonNull PlaylistInfo oldItem, @NonNull PlaylistInfo newItem) {
            return oldItem.equals(newItem);
        }
    };

    @NonNull
    @Override
    public PlaylistManageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_playlist_management_row, parent, false);
        return new PlaylistManageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PlaylistManageViewHolder holder, int position) {
        PlaylistInfo playlistInfo = getItem(position);
        if (playlistInfo != null) {
            holder.bind(playlistInfo, clickListener, optionsClickListener);
        }
    }

    static class PlaylistManageViewHolder extends RecyclerView.ViewHolder {
        ImageView ivCover;
        TextView tvName;
        TextView tvSongCount;
        ImageButton btnOptions;

        PlaylistManageViewHolder(View itemView) {
            super(itemView);
            ivCover = itemView.findViewById(R.id.imageView_playlist_manage_cover);
            tvName = itemView.findViewById(R.id.textView_playlist_manage_name);
            tvSongCount = itemView.findViewById(R.id.textView_playlist_manage_song_count);
            btnOptions = itemView.findViewById(R.id.imageButton_playlist_manage_options);
        }

        void bind(final PlaylistInfo playlistInfo,
                  final OnPlaylistManageClickListener clickListener,
                  final OnPlaylistOptionsClickListener optionsClickListener) {
            tvName.setText(playlistInfo.name);
            tvSongCount.setText(String.format(Locale.getDefault(), "%d 首歌曲", playlistInfo.songCount));

            Context context = itemView.getContext();
            Glide.with(context)
                    .load(playlistInfo.displayCoverArtUri != null ? Uri.parse(playlistInfo.displayCoverArtUri) : null)
                    .placeholder(R.drawable.ic_default_playlist_cover)
                    .error(R.drawable.ic_default_playlist_cover)
                    .into(ivCover);

            itemView.setOnClickListener(v -> {
                if (clickListener != null) {
                    clickListener.onPlaylistManageClick(playlistInfo);
                }
            });

            btnOptions.setOnClickListener(v -> {
                if (optionsClickListener != null) {
                    optionsClickListener.onPlaylistOptionsClick(playlistInfo, btnOptions); // 传递锚点视图
                }
            });
        }
    }
}
